use std::ops::Deref;

#[derive(Debug)]
struct A <T>{
    a: T
}

impl <T> Deref for A<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.a
    }
    
}

fn f1(s: &String) {
    println!("{}", s);
}

fn f2(s: &A::<String>) {
    println!("{}", s.a);
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn test1() {
        let a = A{a: String::from("value")};
        println!("{:?}", a);
        f1(&a);

        f2(&a);
    }
}
/// 要想实现引用，就需要一个泛型的结构体来做例子
struct MyBox<T>(T);

impl <T> Deref for MyBox<T> {
    type Target= T;

    /// Dereferences the value.

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}


#[cfg(test)] 
mod tests {
    use super::*;

    /// 主要是演示了 变量的自动解引用
    ///
    #[test]
    fn test_deref() {
        let s = MyBox(5);
        assert_eq!(*s, 5);
    }

 
}